import { createRouter, createWebHistory, RouteRecordRaw, _RouteLocationBase } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import cache from '@/utils/cache/cache'
import store from '@/store'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'home',
    component: HomeView
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  },
  {
    path: '/songList',
    name: 'songList',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "songList" */ '../views/recommendSongList.vue')
  },
  {
    path: '/search',
    name: 'search',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "search" */ '../views/Search_.vue')
  },
  {
    path: '/login',
    name: 'login',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "search" */ '../views/infoLogin.vue')
  },
  {
    path: '/userList',
    name: 'userList',
    beforeEnter: (to, from, next) => {
      if (cache.getCache('token')) {
        next()
      } else {
        next('/login')
      }
    },
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "search" */ '../views/userLikeSongList.vue')
  },
  {
    path: '/user',
    name: 'user',
    // beforeEnter: (to, from, next) => {
    //   if (cache.getCache('token')) {
    //     next()
    //   } else {
    //     next('/login')
    //   }
    // },
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "search" */ '../views/infoUser.vue')
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
router.beforeEach((to, from) => {
  if (to.path === '/login') {
    store.commit('songList/changeIsMP', false)
  } else {
    store.commit('songList/changeIsMP', true)
  }
})
export default router
